using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace SAK.UI.WebControls
{
  /// <summary>
  /// Questo controllo sostituisce il search box standard emettendo markup HTML accessibile
  /// </summary>
  [DefaultProperty("Text")]
  [ToolboxData("<{0}:SiteSearch runat=server></{0}:SiteSearch>")]
  public class SiteSearch : System.Web.UI.WebControls.WebControl
  {
    /// <summary>
    /// Percorso della pagina che visualizza i risultati della ricerca
    /// </summary>
    [Bindable(true)]
    [Category("Search")]
    [DefaultValue("")]
    [Localizable(true)]
    public string SearchURL
    {
      get
      {
        object s = ViewState["SearchURL"];
        return ((s == null) ? "/SearchCenter/Pagine/Results.aspx" : s.ToString());
      }

      set
      {
        ViewState["SearchURL"] = value;
      }
    }

    /// <summary>
    /// Percorso dell'immagine visualizzata nel controllo di ricerca
    /// </summary>
    [Bindable(true)]
    [Category("Search")]
    [DefaultValue("")]
    [Localizable(true)]
    public string SearchButtonImageURL
    {
      get
      {
        String s = (String)ViewState["SearchButtonImageURL"];
        return ((s == null) ? "/images/Search_Arrow.jpg" : s);
      }

      set
      {
        ViewState["SearchButtonImageURL"] = value;
      }
    }

    /// <summary>
    /// Testo di suggerimento del controllo di ricerca
    /// </summary>
    [Bindable(true)]
    [Category("Search")]
    [DefaultValue("")]
    [Localizable(true)]
    public string SearchButtonTooltips
    {
      get
      {
        String s = (String)ViewState["SearchButtonTooltips"];
        return ((s == null) ? string.Empty : s);
      }

      set
      {
        ViewState["SearchButtonTooltips"] = value;
      }
    }

    /// <summary>
    /// Etichetta del pulsante di ricerca (se non specificato il valore di SearchButtonImageURL)
    /// </summary>
    [Bindable(true)]
    [Category("Search")]
    [DefaultValue("")]
    [Localizable(true)]
    public string SearchLabel
    {
      get
      {
        String s = (String)ViewState["SearchLabel"];
        return ((s == null) ? "Cerca" : s);
      }
      set
      {
        ViewState["SearchLabel"] = value;
      }
    }

    /// <summary>
    /// Class CSS applicata all'etichetta del controllo di ricerca
    /// </summary>
    [Bindable(true)]
    [Category("Search styles")]
    [DefaultValue("")]
    [Localizable(true)]
    public string CssClassLabel
    {
      get
      {
        String s = (String)ViewState["CssClassLabel"];
        return ((s == null) ? string.Empty : s);
      }
      set
      {
        ViewState["CssClassLabel"] = value;
      }
    }

    /// <summary>
    /// Class CSS applicata al campo di ricerca
    /// </summary>
    [Bindable(true)]
    [Category("Search styles")]
    [DefaultValue("")]
    [Localizable(true)]
    public string CssClassTextBox
    {
      get
      {
        String s = (String)ViewState["CssClassTextBox"];
        return ((s == null) ? string.Empty : s);
      }
      set
      {
        ViewState["CssClassTextBox"] = value;
      }
    }

    /// <summary>
    /// Class CSS applicata al pulsante di ricerca (se non specificato il valore di SearchButtonImageURL)
    /// </summary>
    [Bindable(true)]
    [Category("Search styles")]
    [DefaultValue("")]
    [Localizable(true)]
    public string CssClassButton
    {
      get
      {
        String s = (String)ViewState["CssClassButton"];
        return ((s == null) ? string.Empty : s);
      }
      set
      {
        ViewState["CssClassButton"] = value;
      }
    }

    /// <summary>
    /// Render del controllo di ricerca
    /// </summary>
    /// <param name="output"></param>
    public override void RenderControl(HtmlTextWriter output)
    {
      string _buttonID = this.UniqueID + "_button";
      string _buttonClientID = this.ClientID + "_button";
      string _value = string.Empty;

      if (this.Page.IsPostBack)
      {
        if (this.Page.Request[this.UniqueID] != null)
        {
          _value = this.Page.Request[this.UniqueID];
          if (_value.Length > 0 && ((this.Page.Request[_buttonID + ".x"] != null) || (this.Page.Request[_buttonID] != null)))
          {
            this.Page.Response.Redirect(this.SearchURL + "?k=" + _value);
          }
        }
      }

      output.Write("<label class=\"" + this.CssClassLabel + "\" for=\"" + this.ClientID + "\">" + this.SearchLabel + "</label>");
      output.Write("<input class=\"" + this.CssClassTextBox + "\" title=\"" + this.ToolTip + "\" " + (this.AccessKey.Length > 0 ? "accesskey=\"" + this.AccessKey + "\" " : string.Empty) + "type=\"text\" name=\"" + this.UniqueID + "\" id=\"" + this.ClientID + "\" value=\"" + _value + "\" />");

      string _tt = this.SearchButtonTooltips;
      if (string.IsNullOrEmpty(_tt))
        _tt = string.Empty;

      if (this.SearchButtonImageURL.Length > 0)
        output.Write("<input class=\"" + this.CssClassButton + "\" type=\"image\" value=\"\" name=\"" + _buttonID + "\" id=\"" + _buttonClientID + "\" title=\"" + _tt + "\" alt=\"" + _tt + "\" src=\"" + this.SearchButtonImageURL + "\" />");
      else
        output.Write("<input class=\"" + this.CssClassButton + "\" type=\"submit\" title=\"" + _tt + "\" value=\"Trova\" name=\"" + _buttonID + "\" id=\"" + _buttonClientID + "\" />");
    }
  }
}
